# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# Name:         woniusalestest01
# Description:  
# Author:       Administrator
# Date:         2020/2/10
#-------------------------------------------------------------------------------
# 调用工具类的get_txt方法获取配置文件中的信息
from guitest.woniusales_test01.util.utility import Utility
import time
class WoniuSales01:

	# 生成driver
	def __init__(self,path):

		# 通过类名访问类方法
		contents = Utility.get_json(path)
		from selenium import webdriver
		# 通过反射机制直接获取浏览器驱动对象
		self.driver =getattr(webdriver, contents['BROWSER'])()
		self.driver.implicitly_wait(10)
		self.driver.maximize_window()

	# 打开页面
	def open_page(self,path):
		contents = Utility.get_json(path)
		# http://192.168.1.4:8080/WoniuSales
		URL = 'http://%s:%s/%s' %(contents['HOSTNAME'],contents['PORT'],contents['AURL'])
		self.driver.get(URL)

	# 对登录进行测试
	def test_login(self,path):
		# 思路：1.获取测试数据；2.遍历执行每一条测试数据得到运行结果；
		# 3.实际结果actual与预期结果进行对比，如果一直证明测试通过，否则测试不通过提交缺陷
		# 获取所有的登录用到的测试数据及预期结果
		driver = self.driver
		login_info = Utility.trans_txt_dict(path)
		for login_data in login_info:

			from guitest.woniusales_test01.common.login import Login
			Login().do_login(driver,login_data['username'],login_data['password'],login_data['verifycode'])

			# 判断登录是否成功,同时得到实际结果actual
			from selenium.webdriver.common.by import By
			if Utility.is_element_present(driver,By.LINK_TEXT,'注销') :
				actual = 'login-success'
				driver.find_element_by_link_text('注销').click()
				time.sleep(2)
			else:
				actual = 'login-fail'
				time.sleep(2)
				driver.refresh()

			# 对结果进行断言
			if actual == login_data['expect']:
				print('test pass')
			else:
				print('test fail')





if __name__ == '__main__':
	ws = WoniuSales01('config\\base.conf')
	ws.open_page('config\\base.conf')
	ws.test_login('testdata\\login_data')